package com.ungao.tools.generator.engine;

import com.ungao.tools.generator.engine.config.DefaultTemplateConfig;
import com.ungao.tools.generator.engine.config.DefaultVmConfig;
import com.ungao.tools.generator.engine.config.TemplateConfig;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractTemplateEngine implements TemplateEngine {

    /**
     * 默认内置对象
     */
    private final String VM = "vm";
    private final String BUILDER = "config";

    /**
     * 默认配置信息
     */
    protected final String defaultTemplatePath = "/templates";
    protected final String defaultCharset = StandardCharsets.UTF_8.name();
    protected Map<String, Object> config = new ConcurrentHashMap<>();

    /**
     * 初始化模板配置文件
     *
     * @param configBuilder
     * @return
     */
    @Override
    public AbstractTemplateEngine init(TemplateConfig configBuilder) throws Exception {
        Objects.requireNonNull(configBuilder, "this configBuilder is not to be null!");
        this.config.putIfAbsent(this.VM, DefaultVmConfig.getInstance());
        this.config.putIfAbsent(this.BUILDER, new DefaultTemplateConfig(configBuilder).config());
        return this;
    }
}
